//Chen0125 X_X
//LiteLoaderScript Dev Helper
/// <reference path="c:\Users\Administrator\.vscode\extensions/dts/llaids/src/index.d.ts"/> 
var ver = "1.0.0";
var p_n = "X_X";
ll.registerPlugin("X_X","Multifunction",[1,0,0]);
var config = {
    "lang":{
        "zh_CN":{
            "un":"此模块未启用",
            "xr":[
                "xr",
                "重载插件"
            ],
            "xkick":[
                "xkick",
                "踢出玩家",
                ""
            ],
            "motd":[
                "motd",
                "设置服务器标题"
            ],
            "scale":[
                "scale",
                "设置实体体积大小"
            ],
            "menu":[
                "menu",
                "打开表单",
                "此文件不存在",
                "您的权限不足"
            ],
            "command":[
                "您的权限不足",
                "rcmd",
                "重载command"
            ]
        },
        "en":{
            "un":"Not true",
            "xr":[
                "xr",
                "Reload X_X"
            ],
            "xkick":[
                "xkick",
                "Kick players",
                ""
            ],
            "motd":[
                "motd",
                "Set motd"
            ],
            "scale":[
                "scale",
                "Set entites' scale"
            ],
            "menu":[
                "menu",
                "Open menu",
                "This file is not ture",
                "Your level can't open it"
            ],
            "command":[
                "Your level can't use it",
                "rcmd",
                "reload command"
            ]
        }
    },
    "Raw":{
       "path":"./plugins/X_X/",
       "lang":"zh_CN",
       "menu_path":"./plugins/X_X/menu/"
    },
    "config":{
        "AutoUpdate":false,
        "xkick":true,
        "motd":true,
        "SetMaxPlayers":true,
        "scale":true,
        "menu":true
    },
    "AutoUpdate":{
        "time":86400000
    },
    "motd":{
        "enable":false,
        "time":1000,
        "motd":[
            "§rX_X",
            "§bX_X",
            "§aX_X"
        ]
    },
    "SetMaxPlayers":{
        "enable":false,
        "max":100
    }
};
//raw
function Raw() {
    if (!File.exists("./plugins/X_X/Raw.json")) {
        File.writeTo("./plugins/X_X/Raw.json", JSON.stringify(config.Raw, null, "\t"));
    };
    return JSON.parse(File.readFrom("./plugins/X_X/Raw.json"));
};

function R(path, data) {
    if (!File.exists(Raw().path + path)) {
        File.writeTo(Raw().path + path, JSON.stringify(data, null, "\t"));
    };
    return JSON.parse(File.readFrom(Raw().path + path));
};

function Re(pl) {
    if (pl == undefined || pl.langCode == undefined) {
        return R("lang/" + Raw().lang, config["lang"][Raw().lang]);
    };
    if (config["lang"][pl.langCode] == undefined) {
        return R("lang/" + "en", config["lang"]["en"]);
    };
    return R("lang/" + pl.langCode, config["lang"][pl.langCode]);
};

ll.export(Raw,"X_X","Raw");
ll.export(R,"X_X","R");
ll.export(Re,"X_X","Re");
//pn
function pn() {
    let n = File.getFilesList("./plugins/");
    for (let i = 0; i < n.length; i++) {
        if (!File.checkIsDir("./plugins/" + n[i])) {
            let fi = new File("./plugins/" + n[i], file.ReadMode);
            if (fi.readLineSync() == "//Chen0125 X_X") {
                return n[i];
            };
            fi.close();
        };
    };
};
//xr
function xr() {
    mc.runcmdEx("ll unload " + p_n);
    mc.runcmdEx("ll load \"./plugins/" + pn() + "\"");
};

function xreload() {
    let cmd = mc.newCommand(Re().xr[0], Re().xr[1], PermType.GameMasters, 0);
    cmd.overload();
    cmd.setCallback((_cmd, _ori, out, res) => {
        xr();
        return;
    });
    cmd.setup();
};
//xk
function xkick() {
    let cmd = mc.newCommand(Re().xkick[0], Re().xkick[1], PermType.GameMasters, 0);
    cmd.mandatory("player", ParamType.Player);
    cmd.optional("reason", ParamType.Message);
    cmd.overload(["player", "reason"]);
    cmd.setCallback((_cmd, _ori, out, res) => {
        if (!R("config.json", config.config).xkick) {
            return out.addMessage(Re(_ori.player).un);
        };
        for(let i = 0; i < res.player.length; i++) {
            if (res.reason == undefined) {
                res.reason = Re(res.player[i]).xkick[2];
            };
            res.player[i].kick(res.reason);
        };
        return;
    });
    cmd.setup();
};
//au
function xu() {
    network.httpGet("https://gitee.com/chen-0125/X_P/raw/master/ver/X_Xver.txt", (status, nver) => {
        if (status == 200 && nver != ver) {
            network.httpGet("https://gitee.com/chen-0125/X_P/raw/master/plugins/X_X.js", (status, njs) => {
                if (status == 200) {
                    let a = pn();
                    File.writeTo("./plugins/" + a, njs);
                    mc.runcmdEx("ll unload " + p_n);
                    mc.runcmdEx("ll load \"./plugins/" + a + "\"");
                };
            });
        };
    });
    setTimeout(xu, R("AutoUpdate/AutoUpdate.json", config.AutoUpdate).time);
};
//motd
var motdn = 0;

function motd() {
    if (R("motd/motd.json", config.motd).enable) {
        mc.setMotd(R("motd/motd.json", config.motd).motd[motdn]);
        motdn += 1;
        if (motdn >= R("motd/motd.json", config.motd).motd.length) {
            motdn = 0;
        }
        setTimeout(motd, R("motd/motd.json", config.motd).time); 
    };
};

function cmotd() {
    let cmd = mc.newCommand(Re().motd[0], Re().motd[1], PermType.GameMasters, 0);
    cmd.mandatory("motd", ParamType.RawText);
    cmd.overload("motd");
    cmd.setCallback((_cmd, _ori, out, res) => {
        if (!R("config.json", config.config).motd) {
            return out.addMessage(Re(_ori.player).un);
        };
        mc.setMotd(res.motd);
        return;
    });
    cmd.setup();
};
//smp
function smp() {
    if (R("SetMaxPlayers/SetMaxPlayers.json", config.SetMaxPlayers).enable && R("SetMaxPlayers/SetMaxPlayers.json", config.SetMaxPlayers).max > mc.getOnlinePlayers().length) {
        mc.setMaxPlayers(mc.getOnlinePlayers().length + 1);
    };
};
//scale
function scale() {
    let cmd = mc.newCommand(Re().scale[0], Re().scale[1], PermType.GameMasters, 0);
    cmd.mandatory("scale", ParamType.Float);
    cmd.optional("entity", ParamType.Actor);
    cmd.overload(["scale", "entity"]);
    cmd.setCallback((_cmd, _ori, out, res) => {
        if (!R("config.json", config.config).scale) {
            return out.addMessage(Re(_ori.player).un);
        };
        if (res.entity == undefined) {
            res.entity = [_ori.entity];
        };
        for(let i = 0; i < res.entity.length; i++) {
            res.entity[i].setScale(res.scale);
        };
        return;
    });
    cmd.setup();
};
//menu
function menu() {
    let cmd = mc.newCommand(Re().menu[0], Re().menu[1], PermType.GameMasters, 0);
    cmd.mandatory("path", ParamType.RawText);
    cmd.optional("player", ParamType.Player);
    cmd.overload(["path", "player"]);
    cmd.setCallback((_cmd, _ori, out, res) => {
        if (!R("config.json", config.config).menu) {
            return out.addMessage(Re(_ori.player).un);
        };
        if (!File.exists(Raw().menu_path + res.path + ".json")) {
            return out.error(Re(_ori.player).menu[2]);
        };
        let f = JSON.parse(File.readFrom(Raw().menu_path + res.path + ".json"));
        if (_ori.player.permLevel < f.level) {
            return out.addMessage(Re(_ori.player).menu[3]);
        };
        if (res.player == undefined) {
            res.player = [_ori.player];
        };
        for(let i = 0; i < res.player.length; i++) {
            switch (f.type) {
                case "ModalForm":
                    res.player[i].sendModalForm(f.title, f.content, f.button[0], f.button[1], (pl, re) => {
                        if (re != undefined) {
                            for(let i = 0; i < f.command.length; i++) {
                                if (f.command[i][0] == re) {
                                    if (f.command[i][2] != "" && f.command[i][2] != undefined) {
                                        switch (f.command[i][1]) {
                                            case "mc":
                                                mc.runcmd(f.command[i][2]);
                                                break;
                                            case "pl":
                                                pl.runcmd(f.command[i][2]);
                                                break;
                                        };
                                    };
                                };
                            };
                        };
                    });
                    return;
                case "SimpleForm":
                    let m = mc.newSimpleForm();
                    m.setTitle(f.title);
                    m.setContent(f.content);
                    for(let i = 0; i < f.button.length; i++) {
                        m.addButton(f.button[i].text, f.button[i].image);
                    };
                    res.player[i].sendForm(m, (pl, id) => {
                        if (id != undefined) {
                            for(let i = 0; i < f.button[id].command.length; i++) {
                                if (f.button[id].command[i][1] != "" && f.button[id].command[i][1] != undefined) {
                                    switch (f.button[id].command[i][0]) {
                                        case "mc":
                                            mc.runcmd(f.button[id].command[i][1]);
                                            break;
                                        case "pl":
                                            pl.runcmd(f.button[id].command[i][1]);
                                            break;
                                    };
                                };
                            };
                        };
                    });
                    return;
                case "CustomForm":
                    let e = mc.newCustomForm();
                    e.setTitle(f.title);
                    for(let i = 0; i < f.content.length; i++) {
                        switch (f.content[i].type) {
                            case "label":
                                e.addLabel(f.content[i].text);
                                break;
                            case "input":
                                e.addInput(f.content[i].title,f.content[i].placeholder,f.content[i].default);
                                break;
                            case "switch":
                                e.addSwitch(f.content[i].title,f.content[i].default);
                                break;
                            case "dropdown":
                                e.addDropdown(f.content[i].title,f.content[i].items,f.content[i].default);
                                break;
                            case "slider":
                                e.addSlider(f.content[i].title,f.content[i].min,f.content[i].max,f.content[i].step,f.content[i].default);
                                break;
                            case "stepSlider":
                                e.addStepSlider(f.content[i].title,f.content[i].items,f.content[i].default);
                                break;
                        }
                    }
                    for(let i = 0; i < res.player.length; i++) {
                        res.player[i].sendForm(e, (pl, dt) => {
                            if (dt != undefined) {
                                for(let i = 0; i < f.command.length; i++) {
                                    for(let a = 0; a < f.content.length; a++) {
                                        f.command[i][1] = f.command[i][1].replace(`dt[${a}]`, dt[a]);
                                    }
                                    if (f.command[i][1] != "" && f.command[i][1] != undefined) {
                                        switch (f.command[i][0]) {
                                            case "mc":
                                                mc.runcmd(f.command[i][1]);
                                                break;
                                            case "pl":
                                                pl.runcmd(f.command[i][1]);
                                                break;
                                        };
                                    };
                                };
                            };
                        });
                    };
                    return;
            };
        };
        return;
    });
    cmd.setup();
};
//load
function load() {
    xreload();
    if (R("config.json", config.config).AutoUpdate) {
        xu();
    };
    if (R("config.json", config.config).xkick) {
        xkick();
    };
    if (R("config.json", config.config).motd) {
        motd();
        cmotd();
    };
    if (R("config.json", config.config).SetMaxPlayers) {
        mc.listen("onTick",() => {smp();});
    };
    if (R("config.json", config.config).scale) {
        scale();
    };
    if (R("config.json", config.config).menu) {
        menu();
    };
};

load();